package com.rpa.server.servicerpaserver.expose.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.rpa.secure.servicerpasecure.common.result.Result;
import com.rpa.server.servicerpaserver.crawler.entity.Course;
import com.rpa.server.servicerpaserver.crawler.entity.dto.CrawlerDto;
import com.rpa.server.servicerpaserver.crawler.service.CourseService;
import com.rpa.server.servicerpaserver.expose.dto.CourseDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName CourseController
 * @Description TODO
 * @Author 86152
 * @Date 2025/2/10 21:52
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/course")
@Slf4j
@Tag(name = "本系统课程相关接口", description = "CourseController")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @PostMapping("/getCourseList")
    @Operation(summary = "更具用户id获取课程信息", description = "getCourseList")
    public Result<List<Course>> getCourseList(@RequestBody CourseDto course){
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Course::getAccountId, course.getAccountId());

        String courseNameOrManagerLike = course.getCourseNameOrManagerLike();
        if (StrUtil.isNotBlank(courseNameOrManagerLike)) {
            queryWrapper.and(wrapper -> wrapper
                    .like(Course::getCourseName, courseNameOrManagerLike)
                    .or()
                    .like(Course::getCoursePrincipal, courseNameOrManagerLike));
        }

        List<Course> list = courseService.list(queryWrapper);
        return Result.success(list);
    }
    @PostMapping("/getCourseById")
    @Operation(summary = "更具课程id获取课程信息", description = "getCourseById")
    public Result<Course> getCourseById(@RequestBody CourseDto course){
        Course byId = courseService.getById(course.getId());
        return Result.success(byId);
    }
    @PostMapping("/deleteCourseById")
    @Operation(summary = "更具课程id删除课程信息", description = "deleteCourseById")
    public Result<Boolean> deleteCourseById(@RequestBody CourseDto course){
        boolean b = courseService.removeById(course.getId());
        return Result.success(b);
    }

}
